package com.qf.blog.job;

import com.qf.blog.entity.DiscussPost;
import com.qf.blog.service.ICommentService;
import com.qf.blog.service.IDiscussPostService;
import com.qf.blog.service.impl.FollowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class postJob {

    @Autowired
    private IDiscussPostService postService;

    @Autowired
    private FollowerService followerService;

    @Autowired
    private ICommentService commentService;

    @Scheduled(cron = "0/10 * * * * ?")//10秒一次
    public void postJob(){
        System.out.println("计算帖子任务开始执行");
        while (postService.getPostQueueSize() > 0) {


            //获取postId
            Integer postId = postService.getPostQueuePop();

            System.out.println("开始计算帖子("+postId+")分数.....");

            //查询帖子是否加精
            DiscussPost post = postService.getById(postId);

            //查询帖子点赞的数量
            Long startCount = followerService.getStartCount(1, postId);

            //查询帖子评论的数量
            commentService.getCommentListByPostId(post);
            Integer commentCount = post.getCommentCount();

            //算法
            Integer x = post.getStatus() == 1 ? 100 : 0 ;//帖子加精给100
            Long y = startCount * 200; //点赞数量*200
            Long z = commentCount * 300L; //评论数量*200  评论的权重比点赞更高

            //根据三个基数计算 计算她们的对数
            double score = Math.log(x + y + z);

            //更新数据库的分值
            post.setScore(score);
            postService.updateById(post);
        }
    }
}
